package phidgetmotion.classifier;

import java.io.Serializable;
import java.text.DecimalFormat;

public class FeatureVector implements Serializable{
	private static final long serialVersionUID = 2838714248119838254L;
	private int classLabel = 0;
	private double[] vector = new double[9];
	private double[] lastInput = new double[3];
	private int[] inputs = new int[3];
	
	public void addInput(int axis, double value){
		inputs[axis]++;
		vector[axis] += value;
		vector[axis+3] += value-lastInput[axis];
		vector[axis+6] += Math.abs(value-lastInput[axis]);
		lastInput[axis] = value;
	}
	
	public void finished(){
		for(int i=0; i<9; i++){
			vector[i] /= inputs[i%3];
		}
		System.out.println(toString());
	}
	
	public double distance(FeatureVector other){
		double sum = 0.0;
		for(int i=0; i<vector.length; i++){
			double dis = other.vector[i]-vector[i];
			sum += dis*dis;
		}
		return Math.sqrt(sum);
	}
	
	public int getClassLabel() {
		return classLabel;
	}

	public void setClassLabel(int classLabel) {
		this.classLabel = classLabel;
	}
	
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		DecimalFormat formater = new DecimalFormat("#0.0000");
		for(int i=0; i<vector.length; i++){
			buffer.append(formater.format(vector[i]));
			buffer.append('\t');
		}
		return buffer.toString();
	}
	
}
